#ifndef HISTORYDATAWIDGET_H
#define HISTORYDATAWIDGET_H

#include <QWidget>
#include <QTableView>
#include <QPushButton>
#include <QComboBox>
#include <QDateEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QSqlTableModel>
#include <QSqlQueryModel>
#include <QGroupBox>
#include <QLabel>
#include <QMessageBox>
#include <QDateTimeEdit>
#include <QToolBar>
#include <QAction>
#include <QChartView>
#include <QLineSeries>
#include <QValueAxis>
#include <QBarSeries>
#include <QBarSet>
#include <QBarCategoryAxis>
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QFileDialog>
#include <QTextStream>
#include <QAxObject>
#include <QSplitter>
#include <QDateTimeAxis>
#include <QHeaderView>
#include <QFuture>
#include <QtConcurrent>
#include "logclass.h"
#include "sqlitemanage.h"

QT_CHARTS_USE_NAMESPACE

class HistoryDataWidget : public QWidget
{
    Q_OBJECT
public:
    explicit HistoryDataWidget(QWidget *parent = nullptr);
    ~HistoryDataWidget();
    void setUserName(QString &name);
    void loadDeviceList();

private slots:
    void onRefreshButtonClicked();
    void onQueryButtonClicked();
    void onExportCsvButtonClicked();
    void onExportExcelButtonClicked();
    void onChartTypeChanged(int index);

private:

    struct QueryParams {
            int deviceId;
            QDateTime startTime;
            QDateTime endTime;

            bool operator==(const QueryParams &other) const {
                return deviceId == other.deviceId &&
                       startTime == other.startTime &&
                       endTime == other.endTime;
            }
        };
    QueryParams m_lastQuery;
    bool m_isQuerying = false;

    bool isDuplicateQuery(int deviceId, const QDateTime &startTime, const QDateTime &endTime);
    void setupUI();
    void setupModels();
    void updateChart();
    bool exportToCsv(const QString &fileName);
    bool exportToExcel(const QString &fileName);

    QTableView *m_tableView;
    QChartView *m_chartView;
    QComboBox *m_deviceComboBox;
    QDateTimeEdit *m_startDateTimeEdit;
    QDateTimeEdit *m_endDateTimeEdit;
    QPushButton *m_queryButton;
    QPushButton *m_refreshButton;
    QPushButton *m_exportCsvButton;
    QPushButton *m_exportExcelButton;
    QComboBox *m_chartTypeComboBox;
    QSqlQueryModel *m_dataModel;
    SqliteManage *m_pSqliteManage;
    QChart *m_chart;
    LogClass *m_pLog;
    QString username;
};

#endif // HISTORYDATAWIDGET_H












